package com.atguigu.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.atguigu.dao.DictDao;
import com.atguigu.entity.Dict;
import com.atguigu.service.DictService;
import org.apache.zookeeper.data.Id;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = DictService.class)
@Transactional
public class DictServiceImpl implements DictService {
    @Autowired
    private DictDao dictDao;
    @Override
    public List<Map<String, Object>> findZnodes(Long id) {
//        根据父id查询该节点下所以的子节点
        List<Dict> dictList=dictDao.findListById(id);
//        创建返回的List
        List<Map<String,Object>> zNodes=new ArrayList<>();
//        遍历dictList
        for (Dict dict : dictList) {
//            返回数据[{ id:2, isParent:true,name:"随意勾选2"}]
//            创建一个map
            Map map=new HashMap();
            map.put("id",dict.getId());
            map.put("name",dict.getName());
//            调用DictDao中判断该节点是否是父节点的方法
            Integer count=dictDao.isParentNode(dict.getId());
            map.put("isParent",count>0 ? true:false);
            zNodes.add(map);
        }
        return zNodes;
    }
// 根据编码获取子节点数据列表
    @Override
    public List<Dict> findListByDictCode(String dictCode) {
        Dict dict = dictDao.getDictByDictCode(dictCode);
            if(dict==null) {return null;}
        List<Dict> listByParentId = this.findListByParentId(dict.getId());
        return  listByParentId;
    }

    @Override
    public String getNameById(Long id) {
        return dictDao.getNameById(id);
    }

    //根据上级id获取子节点数据列表
    @Override
    public List<Dict> findListByParentId(Long id) {
        return dictDao.findListById(id);
    }

}
